<template>
  <div class="login">
    <div class="maincontent">
      <p>喜虫学员管理系统</p>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        @submit.native.prevent="submitForm('ruleForm')"
      >
        <el-form-item label="" prop="username">
          <el-input
            type="text"
            v-model="ruleForm.username"
            autocomplete="off"
            placeholder="账号名"
          >
            <template slot="prepend"><i class="el-icon-user icon"></i></template
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            placeholder="密码"
            show-password
            ><template slot="prepend"
              ><i class="el-icon-lock icon"></i></template
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { mapMutations, mapState } from "vuex";
import { login } from "../api/login";
export default {
  data() {
    // 定义了两个验证器username和password分别对输入的账号、密码进行验证
    let username = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else if (!/^.{2,16}$/.test(value)) {
        callback(new Error("账号在2到16为字符"));
      } else {
        callback();
      }
    };
    let password = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (!/^[0-9]{6,18}$/.test(value)) {
        callback(new Error("密码在6到18为字母数字"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        // 表单验证规则
        username: [
          { validator: username, trigger: "blur" },
          // { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        password: [{ validator: password, trigger: "blur" }],
      },
    };
  },
  computed: {
    ...mapState(['count','userId'])
  },
  methods: {
    ...mapMutations(['updateUser','getUserid']),
    // 表单提交事件函数
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // alert('submit!');
          // 如果正常，则拿到form表单数据访问服务器
          // 根据用户名和密码去查询数据库，如果有值，那么可以访问管理页面
          // 否则提示用户名或者密码错误
          const res = await login(this.ruleForm);
          // console.log(this.ruleForm);
          // console.log(res);
          if (res.data.code == 200) {
            this.updateUser(res.data.data);
            // console.log(res.data.data);
            this.getUserid(res.data.data[0].userId)
            // console.log(this.count,this.userId);
            this.$message({
              type: "success",
              message: "登录成功",
            });
            setTimeout(() =>{
              this.$router.push("/home/student/find");
            }, 2000);
          } else {
            this.$message("账号或者密码错误");
          }
        } else {
          this.$message("账号或者密码格式不正确");
          return false;
        }
      });
    },

    // 重置表单事件函数
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
  width: 100vw;
  height: 100vh;
  position: relative;
  .maincontent {
    transform: translateY(42%);
    p {
      text-align: center;
      font-size: 30px;
      padding-top: 100px;
      margin-bottom: 50px;
      font-weight: bold;
      margin-block-start: 0px;
      transform: translateX(43px);
      color: #ffffff;
      font-family: "\5b8b\4f53";
    }
    .demo-ruleForm {
      width: 30%;
      margin: auto;
      text-align: center;
      .el-form-item {
        .el-input {
          // border: 1px solid #bdbdbd;
          .icon {
            font-size: 18px;
            color: rgb(68, 68, 208);
          }
        }
      }
    }
  }
}
</style>
